#ifndef     __HEAT_TASK_H
#define     __HEAT_TASK_H

#include "../rtos/thread.h"
#include "../rtos/message_queue.h"

namespace heat_task {

class HeatTask : public rtos::Thread<HeatTask, 1024, osPriorityHigh1> {

public:
  enum class State : uint8_t {
        IDLE = 0,
        WORK = 1,
        FAIL = 2,
        TIMEOUT = 3,
        STOP = 4,
    };

    uint8_t state() {
        return static_cast<uint8_t>(m_state);
    }

    void init_start();

    void run();

    void stop() {
        m_mq.post(State::STOP);
    }

    void idle() {
        m_mq.post(State::IDLE);
    }

private:
    void do_idle();
    void do_work();
    void do_timeout();
    void do_stop();

    bool wait(uint32_t timeout = osWaitForever) {
        return osOK == m_mq.poll(m_state, timeout);
    }

    rtos::lazy::MessageQueue<State, 2> m_mq;
    State m_state = State::IDLE;
};

extern HeatTask heatTask;

}

#endif
